<?php
require_once('inc.config.php');
header('Content-Type: text/html');

$schema_listing_sql = "
SELECT
	TABLE_SCHEMA `database`,
	TABLE_NAME `table`,
	COLUMN_NAME `column`,
	COLUMN_TYPE `type`
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
	TABLE_SCHEMA IN(
		'{$source}',
		'{$target}'
	)
ORDER BY
	`database`,
	`table`,
	`column`
;";
$differences = array();
$rs = mysql_query($schema_listing_sql) or die('SQL Error');
if(!mysql_num_rows(($rs)))
{
	die('No Records');
}
while($row = mysql_fetch_assoc($rs))
{
	# print_r($row);
	/**
	 * Array
	 * (
	 *   [database] => DATABASE
	 *   [table] => TABLE
	 *   [column] => COLUMN
	 *   [type] => TYPE
	 * )
	 */

	/**
	 * Do not process raw tables begining with underscores
	 */
	if(preg_match('/^_/is', $row['table']))
	{
		continue;
	}

	$differences[$row['database']][$row['table']][$row['column']] = $row['type'];
}
# print_r($differences);
/**
 * Array
 * (
 *   [DATABASE] => Array
 *   (
 *     [TABLE] => Array
 *       (
 *         [COLUMN] => TYPE
 *       )
 *   )
 * )
 */
if(isset($differences[$target]))
{
	foreach($differences[$target] as $table => $columns)
	{
		foreach($columns as $column => $type)
		{
			if(isset($differences[$source][$table][$column]))
			{
				if($differences[$source][$table][$column] == $type)
				{
					/**
					 * In case of same column types; remove it
					 */
					unset($differences[$source][$table][$column]);
					unset($differences[$target][$table][$column]);
				}
			}
		}
//		# print_r($columns); die();
//		/**
//		 * Remove the column that exists already and does not need to take care of
//		 */
//		if(isset($differences[$source][$table]))
//		{
//			/**
//			 * Remove from both sides
//			 */
//			unset($differences[$target][$table]);
//			unset($differences[$source][$table]);
//		}
	}
}

/**
 * Trim the empty table details
 */
foreach($differences as $source_target => $tables)
{
	foreach($tables as $table => $columns)
	{
		if(!count($differences[$source_target][$table]))
		{
			unset($differences[$source_target][$table]);
		}
	}
}

$create_tables = array();
if(isset($differences[$source]))
{
	foreach($differences[$source] as $table => $column)
	{
		if(count($table))
		{
			$create_tables[] = "CREATE TABLE {$target}.{$table} LIKE {$source}.{$table};";
			$create_tables[] = "    -- INSERT INTO {$target}.{$table} SELECT * FROM {$source}.{$table};";
		}
	}
}

#echo implode("\r\n", $create_tables);
#print_r($differences);
#print_r($differences[$source]);
#print_r($differences[$target]);
?>

<html>
<head>
	<title>Data/Column Differences</title>
	<style>
		thead tr { background-color: #000000; color:#00FFFF; }
		td, th { vertical-align: top; padding: 4px; }
		tr.source { background-color: #0198d1; }
		tr.target { background-color: #59b4d4; }
		table, thead, tbody, tfood, tr, td { border: none; border-collapse: collapse; }
		td { border-bottom: 1px solid #CCCCCC; }
		td.strong { font-weight: bold; }
	</style>
</head>
<body>
<table>
	<thead>
	<tr>
		<th>Table</th>
		<th>Source (<?php echo $source; ?>)</th>
		<th>Target (<?php echo $target; ?>)</th>
	</tr>
	</thead>
	<tbody>
	<?php
	#print_r($differences); die();
	foreach($differences[$source] as $table => $columns):
		$right = array();
		if(array_key_exists($table, $differences[$target]))
		{
			$right = $differences[$target][$table];
			unset($differences[$target][$table]);
		}
	?>
		<tr class="source">
			<td class="strong"><?php echo $table; ?></td>
			<td><?php echo '<pre>', print_r($columns, true), '</pre>'; ?></td>
			<td><?php echo '<pre>', print_r($right, true), '</pre>'; ?></td>
		</tr>
	<?php
	endforeach;
	foreach($differences[$target] as $table => $columns):
	?>
		<tr class="target">
			<td class="strong"><?php echo $table; ?></td>
			<td><?php echo '<pre>', print_r(array(), true), '</pre>'; ?></td>
			<td><?php echo '<pre>', print_r($columns, true), '</pre>'; ?></td>
		</tr>
	<?php endforeach; ?>
	</tbody>
</table>
</body>
</html>